package com.androidme.app.framework.base;

import android.util.Log;
import com.androidme.app.framework.http.BetterHttp;
import com.androidme.app.framework.http.BetterHttpRequest;
import com.androidme.app.framework.http.BetterHttpResponse;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;

import java.util.ArrayList;

public abstract class AndroidMeHttpTask implements Runnable {

    private String uri;
    private HttpMethod method;
    private ArrayList<NameValuePair> formparams = new ArrayList<NameValuePair>();
    private long delayMillis;
    private String ctxName;
    private boolean isMainTask = false;
    private boolean cached = false;


    public AndroidMeHttpTask(String ctxName, String uri, boolean cached) {
        this(ctxName, uri, HttpMethod.GET, null, false, cached);
    }

    public AndroidMeHttpTask(String ctxName, String uri, boolean isMainTask, boolean cached) {
        this(ctxName, uri, HttpMethod.GET, null, isMainTask, cached);
    }

    public AndroidMeHttpTask(String ctxName, String uri, HttpMethod method,
                             ArrayList<NameValuePair> formparams) {
        this(ctxName, uri, method, formparams, 0L);
    }

    public AndroidMeHttpTask(String ctxName, String uri, HttpMethod method,
                             ArrayList<NameValuePair> formparams, boolean isMainTask, boolean cached) {
        this(ctxName, uri, method, formparams, 0L, isMainTask, cached);
    }

    public AndroidMeHttpTask(String ctxName, String uri, HttpMethod method,
                             ArrayList<NameValuePair> formparams, long delayMillis) {
        this.uri = uri;
        this.method = method;
        this.formparams = formparams;
        this.delayMillis = delayMillis;
        this.ctxName = ctxName;
    }

    public AndroidMeHttpTask(String ctxName, String uri, HttpMethod method,
                             ArrayList<NameValuePair> formparams, long delayMillis, boolean isMainTask, boolean cached) {
        this.uri = uri;
        this.method = method;
        this.formparams = formparams;
        this.delayMillis = delayMillis;
        this.ctxName = ctxName;
        this.isMainTask = isMainTask;
        this.cached = cached;
    }

    @Override
    public void run() {
        if (AndroidMeActivity.application.containActiveContext(ctxName)) {
            onStart();
        } else {
            return;
        }
        String response = null;
        Log.v(getClass().getName(), "uri : " + uri);

        if (delayMillis > 0) {
            try {
                Thread.sleep(delayMillis);
            } catch (InterruptedException ie) {
                Log.e(getClass().getName(), "thread sleep error", ie);
            }
        }

        try {
            if (AndroidMeActivity.application.containActiveContext(ctxName)) {
                BetterHttpResponse resp = null;
                if (method == HttpMethod.GET) {
                    BetterHttpRequest req = BetterHttp.get(uri, cached).withTimeout(1000);
                    resp = req.send();
                    response = resp.getResponseBodyAsString();
                } else if (method == HttpMethod.POST) {
                    HttpEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
                    BetterHttpRequest req = BetterHttp.post(uri, entity);
                    resp = req.send();
                    response = resp.getResponseBodyAsString();
                }
                if (resp == null) {
                    throw new Exception("response is null");
                } else if (resp.getStatusCode() < 200 || resp.getStatusCode() >= 400) {
                    onHttpResponseError(resp.getStatusCode());
                }
            } else {
                return;
            }
        } catch (Exception e) {
            Log.e(getClass().getName(), "http connect error!", e);
            if (AndroidMeActivity.application.containActiveContext(ctxName)) {
                onHTTPException();
            } else {
                return;
            }
            return;
        } finally {
            onStop();
        }

        if (AndroidMeActivity.application.containActiveContext(ctxName)) {
            onSuccess(response);
        }
    }

    protected abstract void onHTTPException();

    protected abstract void onHttpResponseError(int statusCode);

    protected abstract void onSuccess(String s);

    protected abstract void onStart();

    protected abstract void onStop();

    public enum HttpMethod {
        GET, POST
    }
}
